import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

declare module 'vue-router' {
  interface RouteMeta {
    title?: string;
    icon?: string;
    linkTo?: string;
    cache?: boolean;
    hidden?: boolean;
    isBreadcrumd?: boolean;
  }
}

export const dynamicRoutes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/layout/index.vue'),
    redirect: '/home',
    children: [
      /*{
        path: '/home',
        name: 'Home',
        component: () => import('@/views/home/index.vue'),
        meta: {
          title: '首页',
          icon: 'ele-HomeFilled',
          cache: false,
          hidden: false,
        }
      },
      {
        path: '/system',
        name: 'System',
        redirect: '/system/menu',
        meta: {
          title: '系统管理',
          icon: 'ele-Setting',
        },
        children: [
          {
            path: '/system/menu',
            name: 'SystemMenu',
            component: () => import('@/views/system/menu/index.vue'),
            meta: {
              title: '菜单管理',
              icon: 'ele-Menu',
              cache: true,
              hidden: false,
            }
          },
          {
            path: '/system/role',
            name: 'SystemRole',
            component: () => import('@/views/system/role/index.vue'),
            meta: {
              title: '角色管理',
              icon: 'ele-IceCream',
              cache: true,
              hidden: false,
            }
          },
          {
            path: '/system/user',
            name: 'SystemUser',
            component: () => import('@/views/system/user/index.vue'),
            meta: {
              title: '用户管理',
              icon: 'ele-User',
              cache: true,
              hidden: false,
            }
          },
        ]
      },
      {
        path: '/goods',
        name: 'Goods',
        redirect: '/goods/list', // 重定向到第一个子路由
        meta: {
          title: '商品管理',
          icon: 'ele-Goods',
        },
        children: [
          {
            path: '/goods/list',
            name: 'GoodsList',
            component: () => import('@/views/goods/list.vue'),
            meta: {
              title: '商品列表',
              icon: 'ele-GoodsFilled',
              cache: true,
              hidden: false,
            }
          },
          {
            path: '/goods/category',
            name: 'GoodsCategory',
            component: () => import('@/views/goods/category.vue'),
            meta: {
              title: '商品分类',
              icon: 'ele-Operation',
              cache: true,
              hidden: false,
            }
          },
        ]
      },
      {
        path: '/mengxuegu',
        name: 'Mengxuegu',
        component: () => import('@/views/link/index.vue'),
        meta: {
          title: '梦学谷官网',
          icon: 'ele-Link',
          cache: false,
          hidden: false,
          linkTo: 'https://www.mengxuegu.com',
        }
      },*/
      // {
      //   path: '/401',
      //   name: 'NoPermission',
      //   component: () => import('@/views/error/401.vue'),
      //   meta: {
      //     title: '401页面',
      //     icon: 'ele-Warning',
      //     cache: true,
      //     hidden: false,
      //   }
      // },
      // {
      //   path: '/:path(.*)*',    //404匹配不存在的路由
      //   name: 'NotFound',
      //   component: () => import('@/views/error/404.vue'),
      //   meta: {
      //     title: '未找到此页面',
      //     cache: true,
      //     hidden: true,
      //   }
      // },
    ]
  }
];

/**
 * 默认路由配置 ：所有用户都可访问的路由，
 * 不需要通过后端接口返回，都需要将下面路由添加到路由表中
 */
export const defaultRoutes: RouteRecordRaw[] = [
  {
    path: '/401',
    name: 'NoPermission',
    component: () => import('@/views/error/401.vue'),
    hidden: true,// 是否隐藏
    meta: {
      title: '401页面',
      icon: 'ele-Warning',
      cache: true,// 是否缓存
    }
  },
  {
    path: '/:path(.*)*', //  404匹配不存在的路由
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    hidden: true,
    meta: {
      title: '未找到此页面',
      cache: true,
    }
  },
]

/**
 * 全屏显示路由组件，不作用到layout布局主区域渲染出口
 * 这个不通过后端接口返回，系统必须存在的路由
 */
const fullscreenRoutes:RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/auth/login.vue'),
    meta: {
      title: '登录',
      hidden: true
    }
  },{
    path: '/homePage',
    name: 'homePage',
    component: () => import('@/views/homePage/index.vue'),
    meta: {
      title: '首页',
      hidden: true
    }
  },
];


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  //routes: dynamicRoutes,// 通过后端接口返回的路由
  // routes: [...dynamicRoutes, ...fullscreenRoutes] // 通过后端接口返回的路由
  routes: [...defaultRoutes, ...fullscreenRoutes] // 默认路由 + 全屏路由
})

export default router
